package com.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.common.mybatis.Page;
import com.common.utils.DateUtils;
import com.common.utils.base.UuidUtil;
import com.entity.Attachment;
import com.entity.AttachmentRelationship;
import com.entity.Audit;
import com.entity.Product;
import com.repository.AttaRelationshipDao;
import com.repository.AuditDao;
import com.repository.ProductDao;
import com.service.base.BaseServiceImp;

@Service("ProductService")
public class ProductServiceImp extends BaseServiceImp<Product> implements ProductService{

	//附件关系
	@Autowired
	private AttaRelationshipDao fjgxDao;

	//产品
	@Autowired
	private ProductDao productDao;
	//产品审核
	@Autowired
	private AuditDao auditDao;
	
	//附件
	@Autowired
	private AttachmentService attServ;

	@Transactional//事务回滚
	@Override
	public void insterFj(Product o, String AttachmentId) {
		o.setProductId(UuidUtil.get32UUID());
		o.setProductReleaseTime(DateUtils.getDate("yyyy/MM/dd HH:mm:ss"));
		if(AttachmentId!=null){
			String fujianArry[];
			if(AttachmentId.contains(",")){
				fujianArry=AttachmentId.split(",");
			}else{
				fujianArry=new String[]{AttachmentId};
			}
			for(int i=0;i<fujianArry.length;i++){
				AttachmentRelationship fujiangx=new AttachmentRelationship();
				fujiangx.setAttachmentRelationshipId(UuidUtil.get32UUID());
				fujiangx.setAssociatedId(o.getProductId());
				fujiangx.setAttachmentId(fujianArry[i]);
				fujiangx.setAssociatedName("product");
				fjgxDao.insert(fujiangx);
			}
		}
		productDao.insert(o);
	}

	@Transactional//事务回滚
	@Override
	public void updateAll(Product o, String AttachmentId,Audit audit) {
		if(AttachmentId!=null){
			String fujianArry[];
			if(AttachmentId.contains(",")){
				fujianArry=AttachmentId.split(",");
			}else{
				fujianArry=new String[]{AttachmentId};
			}
			for(int i=0;i<fujianArry.length;i++){
				AttachmentRelationship fujiangx=new AttachmentRelationship();
				fujiangx.setAttachmentRelationshipId(UuidUtil.get32UUID());
				fujiangx.setAssociatedId(o.getProductId());
				fujiangx.setAttachmentId(fujianArry[i]);
				fujiangx.setAssociatedName("product");
				fjgxDao.insert(fujiangx);
			}
		}
		audit.setProductId(o.getProductId());
		List<Audit> audList=auditDao.find(audit);
		if(audList.size()==0 || audList==null){
			audit.setAuditTime(DateUtils.getDate("yyyy/MM/dd HH:mm:ss"));
			audit.setAuditId(UuidUtil.get32UUID());
			auditDao.insert(audit);
		}else{
			audit.setAuditId(audList.get(0).getAuditId());
			if(!audList.get(0).getAuditStatus().equals(audit.getAuditStatus())){
				audit.setAuditTime(audList.get(0).getAuditTime());
				auditDao.update(audit);
			}
		}
		productDao.update(o);
	}
	
	/**
	 * 根据ProductId获得对象列表
	 * @param o 对象       
	 * @return List
	 */
	public List<Product> findOne(Product o){
		return productDao.findOne(o);
	}

	
	
	@Override
	public Page<Product> findByPageIndustryId(Product o, Page<Product> page,String state,
			String UserIndustryId){
		page.setResults(productDao.findByPageIndustryId(o, page,state, UserIndustryId));
		return page;
	}

	@Override
	public Page<Product> RecommendProduct(Product o, Page<Product> page,
			String UserIndustryId) {
		page.setResults(productDao.RecommendProduct(o, page,UserIndustryId));
		return page;
	}

	@Transactional//事务回滚
	@Override
	public Product findByProId(Product o) {
		Product pr=productDao.findByProId(o).get(0);
		try {
			Attachment att=new Attachment();
			att.setAssociatedName("product");
			att.setAssociatedId(o.getProductId());
			List<Attachment> attList=attServ.find(att);
			pr.setAttachmentPath(attList.get(0).getAttachmentPath());
		} catch (Exception e) {
			// TODO: handle exception
		}
		return pr;
	}


}
